<template>
    <div>
        <h2>用药管理</h2>
        <span>医生姓名</span>
        <input type="text" v-model="quertInfo.Dispensingdoctors">
        <span>科室</span>
        <select v-model="quertInfo.DepartId">
            <option value="">请选择</option>
            <option v-for="item in data1" :key="item.id" :value="item.id">{{item.department}}</option>
        </select>
        <input type="button" value="查询" @click="getMedctionList" class="btn btn-primary">
        <input type="button" value="批量完成" @click="getUpt(1)" class="btn btn-danger">
        <input type="button" value="批量未完成" @click="getUpt(0)" class="btn btn-success">
        <input type="button" value="导出到Excel" @click="Eximport" class="btn btn-warning">
        <table class="table table-border">
            <tbody>
                <tr class="bg bg-primary">
                    <td><input type="checkbox" v-model="checkAll" @click="gocheckAll" /></td>
                    <td>药品名称</td>
                    <td>科室</td>
                    <td>用药日期</td>
                    <td>病人姓名</td>
                    <td>病房号码</td>
                    <td>用药状态</td>
                    <td>配药医生</td>
                    <td>操作</td>
                </tr>
                <tr v-for="item in data.list" :key="item.id">
                    <td><input type="checkbox" v-model="ids" :value="item.id" @change="checkone" /></td>
                    <td>{{item.medicalName}}</td>
                    <td>
                        <span v-if="item.departId == 1">耳科</span>
                        <span v-if="item.departId == 2">鼻科</span>
                        <span v-if="item.departId == 3">耳鼻科</span>
                        <span v-if="item.departId == 4">眼科</span>
                        <span v-if="item.departId == 5">内科</span>
                        <span v-if="item.departId == 6">外科</span>
                        <span v-if="item.departId == 7">耳鼻喉科</span>
                        <span v-if="item.departId == 8">儿科</span>
             
                    </td>
                    <td>{{moment(item.medicatime).format('YYYY-MM-DD')}}</td>
                    <td>{{item.patientName}}</td>
                    <td>{{item.patientNumber}}</td>
                    
                    <td v-if="item.state==1" style="color:green">已完成</td>
                    <td v-if="item.state==0" style="color:red">未开始</td>
                    <td>{{item.dispensingdoctors}}</td>
                    <td>
                       <a @click="toDetail(item.id)">修改</a>
                       <a style="color: brown;" @click="getDel(item.id)">删除</a>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <div>
        <span>
            共<a style="color: red">{{data.totalCount}}</a>条数据
        </span>
        <span>每页<a style="color: red;">{{ data.totalPage }}</a>条</span>
        <span>共<a style="color: red;">{{quertInfo.Pageindex}}</a>/<a style="color: red;">{{data.totalPage}}</a>页</span>
        <input type="button" value="首页" @click="gopage('a')"/>
        <input type="button" value="上一页" @click="gopage('b')"/>
        <span v-for="item in data.totalPage" :key="item" @click="gopage(item)">{{item}}</span>
        <input type="button" value="下一页" @click="gopage('c')"/>
        <input type="button" value="尾页" @click="gopage('d')"/>
    </div>
</template>

<script setup lang="ts">
import {ref, reactive,onMounted} from 'vue'
import {useRouter, useRoute} from 'vue-router'
import axios from 'axios';
import moment from 'moment';
let router = useRouter();
let route = useRoute();

//科室管理
let data1:any= ref([])
onMounted(()=>{
    GetDepartmentList();
    getMedctionList();//科室管理
})
let quertInfo1= reactive({
    departmen: '',
})
const GetDepartmentList = () => {
    axios({
        url: '/api/Department/GetDepartmentList',
        method: 'get',
        params: quertInfo1
    })
    .then((res) => {
        console.log(res);
        console.log(quertInfo);
        data1.value = res.data;
    })
}



//用药管理列表
let data:any=reactive({
    list:[],
    totalCount:0,
    totalPage:0,
})
let quertInfo=reactive({
    Pageindex:1,
    pageSize:2,
    Dispensingdoctors:"",
    DepartId:"",
})

const getMedctionList=()=>{
    axios({
        url:"/api/Medication/GetMedicatioList",
        method:"get",
        params:quertInfo
    })
    .then((res)=>{
        console.log(res);
        data.list=res.data.list;
        data.totalCount=res.data.totalCount;
        data.totalPage=res.data.totalPage;
    })
}
const gopage=(str:any)=>{
    switch(str){
        case 'a':
            quertInfo.Pageindex=1;
            break;
        case 'b':
            quertInfo.Pageindex=quertInfo.Pageindex-1;
            break;
        case 'c':
            quertInfo.Pageindex=quertInfo.Pageindex+1;
            break;
        case 'd':
            quertInfo.Pageindex=data.totalPage;
            break;
         case str:
            quertInfo.Pageindex=str;
            break;
    }
    getMedctionList();
}
//选择
let ids=ref([]);
let checkAll=ref(false);
const gocheckAll=()=>{
    checkAll.value=!checkAll.value;
    if(checkAll.value){
        ids.value=data.list.map((item:any)=>{
            return item.id;
        })
    }
    else{
        ids.value=[];
    }
}
const checkone=() =>{
    if(ids.value.length==data.list.length){
        checkAll.value=true;
    }
    else{
        checkAll.value=false;
    }
}
//批量修改状态
const getUpt=(state:any)=>{
    axios({
        url:'/api/Medication/MedicaUptAll',
        method:'put',
        params:{
            ids:ids.value.toString(),
            state:state
        }
    })
    .then((res)=>{
        if(res.data>0){
            console.log(res);
            alert('修改成功');
            getMedctionList();
        }
        else{
            alert('修改失败');
        }
    })
    .then((err)=>{
        console.log(err);
    })
}
const getDel=(id:any)=>{
    var f=confirm('确定要删除吗？');
    if(f){
        axios({
        url:'/api/Medication/MedicaDelete',
        method:'delete',
        params:{
            id:id
        }
    })
    .then((res)=>{
        if(res.data>0){
            alert('删除成功');
            getMedctionList();  
        }
        else{
            alert('删除失败');
        }
    })

    }
}
const toDetail=(id:any)=>{
    router.push({path:'/MedicationUpdate',query:{id:id}})
}
//导出
const Eximport=()=>{
    location.href=`https://localhost:7163/api/Medication/Exslsxinmp?Pageindex=${quertInfo.Pageindex}&pageSize=${quertInfo.pageSize}`;
}
</script>

<style scoped>
span{
    margin: 0 10px;
}

input[type="button"]{
    margin:10px;
}
a{
    margin: 10px;
}
</style>